home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.02 Feb 91 / Jorg Source / MacTutorApp.cp < prev    next >
Encoding:
Text File  |  1990-12-18  |  6.2 KB  |  279 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2. #    MacTutorApp
  3. #
  4. #    A rudimentary application skeleton
  5. #    J. Langowski / MacTutor 1989
  6. #------------------------------------------------------------------------------*/
  7. #include <Types.h>
  8. #include <QuickDraw.h>
  9. #include <Fonts.h>
  10. #include <Events.h>
  11. #include <OSEvents.h>
  12. #include <Controls.h>
  13. #include <Windows.h>
  14. #include <Menus.h>
  15. #include <TextEdit.h>
  16. #include <Dialogs.h>
  17. #include <Desk.h>
  18. #include <Scrap.h>
  19. #include <ToolUtils.h>
  20. #include <Memory.h>
  21. #include <SegLoad.h>
  22. #include <Files.h>
  23. #include <OSUtils.h>
  24. #include <Traps.h>
  25. #include <StdLib.h>
  26.  
  27. #include "TDocument.h"
  28. #include "TApplication.h"
  29. #include "MacTutorApp.h"
  30. #include "MacTutorDoc.h"
  31. #include "MacTutorGrow.h"
  32.  
  33. pascal void initFortran();
  34. pascal void exitFortran();
  35.  
  36. // Methods for our application class
  37. TMacTutorApp::TMacTutorApp(void)
  38. {
  39.     Handle    menuBar;
  40.  
  41.     initFortran(); // initialize Fortran runtime system
  42.     
  43.     // read menus into menu bar
  44.     menuBar = GetNewMBar(rMenuBar);
  45.     // install menus
  46.     SetMenuBar(menuBar);
  47.     DisposHandle(menuBar);
  48.     // add DA names to Apple menu
  49.     AddResMenu(GetMHandle(mApple), 'DRVR');
  50.     DrawMenuBar();
  51.  
  52.     // create empty mouse region
  53.     fMouseRgn = NewRgn();
  54.     // create a single empty document
  55.     DoNew();
  56. }
  57.  
  58. TMacTutorApp::~TMacTutorApp(void)
  59. {
  60.     exitFortran(); // exit Fortran runtime system
  61. }
  62.  
  63. // Tell TApplication class how much heap we need
  64. long TMacTutorApp::HeapNeeded(void)
  65. {
  66.     return (kMinSize * 1024);
  67. }
  68.  
  69. // Calculate a sleep value for WaitNextEvent. This takes into account the things
  70. // that DoIdle does with idle time.
  71.  
  72. unsigned long TMacTutorApp::SleepVal(void)
  73. {
  74.     unsigned long sleep;
  75.     const long kSleepTime = 0x7fffffff;    // a very large positive number
  76.  
  77.     sleep = kSleepTime;                // default value for sleep
  78.     if ((!fInBackground))
  79.     {
  80.           sleep = GetCaretTime();    // A reasonable time interval for MenuClocks, etc.
  81.     }
  82.     return sleep;
  83. }
  84.  
  85. void TMacTutorApp::AdjustMenus(void)
  86. {
  87.     WindowPtr    frontmost;
  88.     MenuHandle    menu;
  89.     Boolean undo,cutCopyClear,paste;
  90.  
  91.     TMacTutorDocument* fMacTutorCurDoc = (TMacTutorDocument*) fCurDoc;
  92.  
  93.     frontmost = FrontWindow();
  94.  
  95.     menu = GetMHandle(mFile);
  96.     if ( fDocList->NumDocs() < kMaxOpenDocuments )
  97.       EnableItem(menu, iNew);            // New is enabled when we can open more documents 
  98.     else DisableItem(menu, iNew);
  99.     if ( frontmost != (WindowPtr) nil )    // Close is enabled when there is a window to close 
  100.       EnableItem(menu, iClose);
  101.     else DisableItem(menu, iClose);
  102.  
  103.     undo = false;
  104.     cutCopyClear = false;
  105.     paste = false;
  106.     
  107.     if ( fMacTutorCurDoc == nil )
  108.       {
  109.         undo = true;                // all editing is enabled for DA windows 
  110.         cutCopyClear = true;
  111.         paste = true;
  112.       }
  113.       
  114.     menu = GetMHandle(mEdit);
  115.     if ( undo )
  116.         EnableItem(menu, iUndo);
  117.     else
  118.         DisableItem(menu, iUndo);
  119.     
  120.     if ( cutCopyClear )
  121.       {
  122.         EnableItem(menu, iCut);
  123.         EnableItem(menu, iCopy);
  124.         EnableItem(menu, iClear);
  125.       } 
  126.     else
  127.       {
  128.         DisableItem(menu, iCut);
  129.         DisableItem(menu, iCopy);
  130.         DisableItem(menu, iClear);
  131.       }
  132.       
  133.     if ( paste )
  134.         EnableItem(menu, iPaste);
  135.     else
  136.         DisableItem(menu, iPaste);
  137.         
  138.     menu = GetMHandle(myMenu);
  139.         EnableItem(menu, item1);
  140.         EnableItem(menu, item2);
  141.         EnableItem(menu, item3);
  142.         EnableItem(menu, item5);
  143.  
  144.         CheckItem(menu, item1, false);
  145.         CheckItem(menu, item2, false);
  146.         CheckItem(menu, item3, false);
  147.         CheckItem(menu, item5, false);
  148.         CheckItem(menu, fMacTutorCurDoc->GetItemSelected(), true);
  149. } // AdjustMenus
  150.  
  151. void TMacTutorApp::DoMenuCommand(short menuID, short menuItem)
  152. {
  153.     short        itemHit;
  154.     Str255        daName;
  155.     short        daRefNum;
  156.     float        x;    // for testing the Fortran call
  157.     WindowPtr    window;
  158.     TMacTutorDocument* fMacTutorCurDoc = (TMacTutorDocument*) fCurDoc;
  159.  
  160.     window = FrontWindow();
  161.     switch ( menuID )
  162.       {
  163.         case mApple:
  164.             switch ( menuItem )
  165.               {
  166.                 case iAbout:        // About box
  167.                     itemHit = Alert(rAboutAlert, nil);
  168.                     break;
  169.                 default:            // DAs etc.
  170.                     GetItem(GetMHandle(mApple), menuItem, daName);
  171.                     daRefNum = OpenDeskAcc(daName);
  172.                     break;
  173.               }
  174.             break;
  175.         case mFile:
  176.             switch ( menuItem )
  177.               {
  178.                 case iNew:
  179.                     DoNew();
  180.                     break;
  181.                 case iClose:
  182.                     if (fMacTutorCurDoc != nil)
  183.                       {
  184.                         fDocList->RemoveDoc(fMacTutorCurDoc);
  185.                         delete fMacTutorCurDoc;
  186.                       }
  187.                     else CloseDeskAcc(((WindowPeek) fWhichWindow)->windowKind);
  188.                     break;
  189.                 case iQuit:
  190.                     Terminate();
  191.                     break;
  192.               }
  193.             break;
  194.         case mEdit:                    // call SystemEdit for DA editing & MultiFinder 
  195.             if ( !SystemEdit(menuItem-1) )
  196.               {
  197.                 switch ( menuItem )
  198.                   {
  199.                     case iCut:
  200.                         break;
  201.                     case iCopy:
  202.                         break;
  203.                     case iPaste:
  204.                         break;
  205.                     case iClear:
  206.                         break;
  207.                    }
  208.               }
  209.             break;
  210.         case myMenu:
  211.             if (fMacTutorCurDoc != nil) 
  212.             {
  213.                 switch ( menuItem )
  214.                   {
  215.                     case item1:
  216.                         fMacTutorCurDoc->SetDisplayString("\pC++");
  217.                         break;
  218.                     case item2:
  219.                         fMacTutorCurDoc->SetDisplayString("\pSample");
  220.                         break;
  221.                     case item3:
  222.                         fMacTutorCurDoc->SetDisplayString("\pApplication");
  223.                         break;
  224.                     case item5:
  225.                         fMacTutorCurDoc->SetDisplayString("\pHave Fun");
  226.                             
  227.                             x = 4.567;
  228.                             fMacTutorCurDoc->FtnCall(&menuItem,3456,&x);
  229.  
  230.                             break;
  231.                    }
  232.             fMacTutorCurDoc->SetItemSelected(menuItem);
  233.             InvalRect(&(window->portRect));
  234.             fMacTutorCurDoc->DoUpdate();
  235.             }
  236.             break;
  237.       }
  238.     HiliteMenu(0);
  239. } // DoMenuCommand
  240.  
  241. // Create a new document and window. 
  242. void TMacTutorApp::DoNew(void)
  243. {
  244.     TMacTutorGrow* tMacTutorDoc;
  245.     
  246.     tMacTutorDoc = new TMacTutorGrow(rDocWindow,"\pNothing selected yet.");
  247.     // if we didn't get an allocation error, add it to list
  248.     if (tMacTutorDoc != nil)
  249.       fDocList->AddDoc(tMacTutorDoc);
  250. } // DoNew
  251.  
  252. void TMacTutorApp::Terminate(void)
  253. {
  254.     ExitLoop();
  255. } // Terminate
  256.  
  257.  
  258. // Our application object, initialized in main(). We make it
  259. // global so our functions which don't belong to any class
  260. // can find the active document.
  261. TMacTutorApp *gTheApplication;
  262.  
  263. // main is the entrypoint to the program
  264. int main(void)
  265. {
  266.     // Create our application object. This MUST be the FIRST thing
  267.     // done in main(), since it initializes the Toolbox for us.
  268.     gTheApplication = new TMacTutorApp;
  269.     if (gTheApplication == nil)        // if we couldn't allocate object (impossible!?)
  270.       return 0;                        // go back to Finder
  271.     
  272.     // Start our main event loop running. This won't return until user quits
  273.     gTheApplication->EventLoop();
  274.  
  275.     // We always return a value, like good little ANSI worshippers
  276.     return 0;
  277. }
  278.  
  279.